package com.manong.web.controller;

import com.github.pagehelper.PageInfo;
import com.manong.data.ResultData;
import com.manong.domain.Order;
import com.manong.query.OrderQueryObject;
import com.manong.service.IOrderService;
import com.manong.util.JsonResult;
import com.manong.util.RequiredPermission;
import com.manong.vo.OrderStoreVo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/order")
@CrossOrigin(allowCredentials = "true")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单列表
     * @param queryObject
     * @return
     */
    @GetMapping("/list")
    @RequiredPermission(name = "查询订单列表", expression = "order:getList")
    public JsonResult list(OrderQueryObject queryObject) {
        PageInfo pageInfo =  orderService.query(queryObject);
        ResultData data = new ResultData(queryObject.getPageNum(),queryObject.getPageSize(),pageInfo.getList(),Integer.parseInt(pageInfo.getTotal()+""));
        return JsonResult.success(data);
    }

    /**
     * 删除订单接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiredPermission(name = "订单删除", expression = "order:delete")
    public JsonResult delete(@PathVariable Integer id) {
        orderService.delete(id);
        return JsonResult.success();
    }


    /**
     * 新增或者修改订单
     * @param order
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @RequiredPermission(name = "订单新增或者编辑", expression = "order:saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody OrderStoreVo order) {
        orderService.saveOrUpdate(order);
        return JsonResult.success();
    }

    /**
     * 确认订单
     * @param order
     * @return
     */
    @PostMapping("/confirmOrder")
    @RequiredPermission(name = "配送员确认订单", expression = "order:confirmOrder")
    public JsonResult confirmOrder(@RequestBody OrderStoreVo order) {
        orderService.confirmOrder(order);
        return JsonResult.success();
    }


    /**
     * 确认入库
     * @param order
     * @return
     */
    @PostMapping("/confirmWarehouse")
    @RequiredPermission(name = "仓库负责人确认入库", expression = "order:confirmWarehouse")
    public JsonResult confirmWarehouse(@RequestBody OrderStoreVo order) {
        orderService.confirmWarehouse(order);
        return JsonResult.success();
    }

    /**
     * 确认入店
     * @param order
     * @return
     */
    @PostMapping("/confirmStore")
    @RequiredPermission(name = "门面负责人确认入店", expression = "order:confirmStore")
    public JsonResult confirmStore(@RequestBody OrderStoreVo order) {
        orderService.confirmStore(order);
        return JsonResult.success();
    }

    /**
     * 查询单个订单
     * @param id
     * @return
     */
    @GetMapping("/getOrder/{id}")
    @RequiredPermission(name = "查询单个订单", expression = "order:getOrder")
    public JsonResult getorder(@PathVariable Integer id) {
        Order order = orderService.get(id);
        // JsonResult data属性 role
        return JsonResult.success(order);
    }

    /**
     * 查询每日订单数量
     * @return
     */
    @GetMapping("/dailyOrderCount")
    @RequiredPermission(name = "查询每日订单数量", expression = "order:dailyOrderCount")
    public JsonResult dailyOrderCount() {
        List<Map<String, Object>> dailyOrderCount = orderService.getDailyOrderCount();
        return JsonResult.success(dailyOrderCount);
    }

    /**
     * 获取全部订单数量
     * @return
     */
    @GetMapping("/totalOrderCount")
    @RequiredPermission(name = "查询全部订单数量", expression = "order:totalOrderCount")
    public JsonResult totalOrderCount() {
        int totalOrderCount = orderService.getTotalOrderCount();
        return JsonResult.success(totalOrderCount);
    }

    /**
     * 根据 origin_id 查询每日订单数量
     * @param originId
     * @return
     */
    @GetMapping("/dailyOrderCountByOriginId/{originId}")
    @RequiredPermission(name = "根据 origin_id 查询每日订单数量", expression = "order:dailyOrderCountByOriginId")
    public JsonResult dailyOrderCountByOriginId(@PathVariable Integer originId) {
        List<Map<String, Object>> dailyOrderCount = orderService.getDailyOrderCountByOriginId(originId);
        return JsonResult.success(dailyOrderCount);
    }

    /**
     * 根据 origin_id 查询不同订单状态的数量
     * @param originId
     * @return
     */
    @GetMapping("/orderStatusCountByOriginId/{originId}")
    @RequiredPermission(name = "根据 origin_id 查询不同订单状态的数量", expression = "order:orderStatusCountByOriginId")
    public JsonResult orderStatusCountByOriginId(@PathVariable Integer originId) {
        List<Map<String, Object>> orderStatusCount = orderService.getOrderStatusCountByOriginId(originId);
        return JsonResult.success(orderStatusCount);
    }
    /**
     * 根据 origin_id 查询 order_type 为 2 的每日订单数量
     * @param originId
     * @return
     */
    @GetMapping("/dailyOrderCountByOriginIdAndOrderType/{originId}")
    @RequiredPermission(name = "根据 origin_id 查询 order_type 为 2 的每日订单数量", expression = "order:dailyOrderCountByOriginIdAndOrderType")
    public JsonResult dailyOrderCountByOriginIdAndOrderType(@PathVariable Integer originId) {
        List<Map<String, Object>> dailyOrderCount = orderService.getDailyOrderCountByOriginIdAndOrderType(originId);
        return JsonResult.success(dailyOrderCount);
    }

    /**
     * 根据 origin_id 查询 order_type 为 2 的不同订单状态的数量
     * @param originId
     * @return
     */
    @GetMapping("/orderStatusCountByOriginIdAndOrderType/{originId}")
    @RequiredPermission(name = "根据 origin_id 查询 order_type 为 2 的不同订单状态的数量", expression = "order:orderStatusCountByOriginIdAndOrderType")
    public JsonResult orderStatusCountByOriginIdAndOrderType(@PathVariable Integer originId) {
        List<Map<String, Object>> orderStatusCount = orderService.getOrderStatusCountByOriginIdAndOrderType(originId);
        return JsonResult.success(orderStatusCount);
    }
}
